using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Extensions;
using Deepcode.Flux.Core.Systems.Addins;

public partial class Private_Config_Addins_Default : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region Permission Code Override
	public override string RequiredPermissionCode { get { return "FluxGeneralAdmin"; } }
	#endregion
	#region Page Load event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			BindDataGrid();
		}
	}
	#endregion
	#region Initialisation
	/// <summary>
	/// OnInit - hooks up the data paging handler, 
	/// rpp change handler and item command handlers
	/// </summary>
	/// <param name="e"></param>
	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);
		datagrid.ItemCommand += new DataGridCommandEventHandler(GridCommand);
		datagrid.ItemDataBound += new DataGridItemEventHandler(datagrid_ItemDataBound);
	}
	#endregion
	#region Data binding and paging events
	/// <summary>
	/// Data binding
	/// </summary>
	private void BindDataGrid()
	{
		// Get and bind the data
		Addin [] addins = AddinTree.Tree.Addins.ToArray(typeof(Addin)) as Addin [];
		datagrid.DataSource = addins;
		datagrid.DataBind();
	}
	/// <summary>
	/// Item being bound to the grid
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void datagrid_ItemDataBound(object sender, DataGridItemEventArgs e)
	{
		if (e.Item.ItemType == ListItemType.Footer ||
			e.Item.ItemType == ListItemType.Header ||
			e.Item.ItemType == ListItemType.Pager ||
			e.Item.ItemType == ListItemType.Separator) return;

		LinkButton btnUpdate = (LinkButton) e.Item.FindControl("btnUpdate");
		Label txtStatus = (Label) e.Item.FindControl("txtStatus");

		Addin addin = (Addin) e.Item.DataItem;
		if( addin == null ) return;

		if (addin.InstallUpdateScript == "")
		{
			txtStatus.CssClass = "SafeLink";
			txtStatus.Text = "Not an updatable addin";
			btnUpdate.Visible = false;
			return;
		}

		KnownAddins ka = new KnownAddins();
		KnownAddin addinDB = ka.GetByGuid( addin.GUID );
		bool needsUpdate = true;

		if (addinDB != null)
		{
			Version DBVersion = new Version(addinDB.VersionString);
			Version CodeVersion = new Version(addin.Version);

			if (DBVersion < CodeVersion) needsUpdate = true;
			else needsUpdate = false;
		}

		// If it's not in the database (0.0.0.0) or the 
		// database version is less than the code version
		// it needs an update
		if (needsUpdate)
		{
			txtStatus.CssClass = "DangerLink";
			txtStatus.Text = "Needs update to " + addin.Version;
			btnUpdate.Visible = true;
		}
		else
		{
			txtStatus.CssClass = "SafeLink";
			txtStatus.Text = "Up to date";
			btnUpdate.Visible = false;
		}
	}
	#endregion
	#region User interactions
	/// <summary>
	/// Command received from the data grid
	/// </summary>
	/// <param name="source"></param>
	/// <param name="e"></param>
	void GridCommand(object source, DataGridCommandEventArgs e)
	{
		if( e.Item.ItemType == ListItemType.Footer ||
			e.Item.ItemType == ListItemType.Header ||
			e.Item.ItemType == ListItemType.Pager ||
			e.Item.ItemType == ListItemType.Separator ) return;

		// Get the key of the row being actioned 
		Guid rowKey = (Guid) datagrid.DataKeys[e.Item.ItemIndex];

		// PROCESS COMMANDS
		if( e.CommandName == "Update" )
		{
			Addin a = AddinTree.Tree.GetAddin( rowKey );
			if( a == null ) return;

			string ManifestFile = a.InstallUpdateScript;
			string Url = ManifestFile;
			
			if( ManifestFile.EndsWith(".setup.config"))
				Url = String.Format("~/admin/setup/process/default.aspx?finish=~/admin/Private/Config/Addins/&ifile={0}", ManifestFile);

			Response.Redirect(Url, true);		
			
			this.BindDataGrid();
			return;
		}
	}
	#endregion
}
